<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.core.validators &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../index.html" />
    <link rel="up" title="django" href="../../django.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-core-validators">
            
  <h1>Source code for django.core.validators</h1><div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">unicode_literals</span>

<span class="kn">import</span> <span class="nn">re</span>

<span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="kn">import</span> <span class="n">ValidationError</span>
<span class="kn">from</span> <span class="nn">django.utils.deconstruct</span> <span class="kn">import</span> <span class="n">deconstructible</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="kn">import</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span><span class="p">,</span> <span class="n">ungettext_lazy</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="kn">import</span> <span class="n">force_text</span>
<span class="kn">from</span> <span class="nn">django.utils.ipv6</span> <span class="kn">import</span> <span class="n">is_valid_ipv6_address</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.six.moves.urllib.parse</span> <span class="kn">import</span> <span class="n">urlsplit</span><span class="p">,</span> <span class="n">urlunsplit</span>


<span class="c"># These values, if given to validate(), will trigger the self.required check.</span>
<span class="n">EMPTY_VALUES</span> <span class="o">=</span> <span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="p">[],</span> <span class="p">(),</span> <span class="p">{})</span>


<span class="nd">@deconstructible</span>
<div class="viewcode-block" id="RegexValidator"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.RegexValidator">[docs]</a><span class="k">class</span> <span class="nc">RegexValidator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="n">regex</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Enter a valid value.&#39;</span><span class="p">)</span>
    <span class="n">code</span> <span class="o">=</span> <span class="s">&#39;invalid&#39;</span>
    <span class="n">inverse_match</span> <span class="o">=</span> <span class="bp">False</span>
    <span class="n">flags</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">regex</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">message</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">inverse_match</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">regex</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">regex</span> <span class="o">=</span> <span class="n">regex</span>
        <span class="k">if</span> <span class="n">message</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">message</span>
        <span class="k">if</span> <span class="n">code</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">code</span> <span class="o">=</span> <span class="n">code</span>
        <span class="k">if</span> <span class="n">inverse_match</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">inverse_match</span> <span class="o">=</span> <span class="n">inverse_match</span>
        <span class="k">if</span> <span class="n">flags</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">flags</span> <span class="o">=</span> <span class="n">flags</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">flags</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">regex</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&quot;If the flags are set, regex must be a regular expression string.&quot;</span><span class="p">)</span>

        <span class="c"># Compile the regex if it was not passed pre-compiled.</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">regex</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">regex</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">regex</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">flags</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Validates that the input matches the regular expression</span>
<span class="sd">        if inverse_match is False, otherwise raises ValidationError.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">inverse_match</span> <span class="ow">is</span> <span class="ow">not</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">regex</span><span class="o">.</span><span class="n">search</span><span class="p">(</span>
                <span class="n">force_text</span><span class="p">(</span><span class="n">value</span><span class="p">)))):</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">RegexValidator</span><span class="p">)</span> <span class="ow">and</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">regex</span><span class="o">.</span><span class="n">pattern</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">regex</span><span class="o">.</span><span class="n">pattern</span> <span class="ow">and</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">regex</span><span class="o">.</span><span class="n">flags</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">regex</span><span class="o">.</span><span class="n">flags</span> <span class="ow">and</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">message</span><span class="p">)</span> <span class="ow">and</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">code</span><span class="p">)</span> <span class="ow">and</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">inverse_match</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">inverse_match</span><span class="p">)</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">__ne__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="p">(</span><span class="bp">self</span> <span class="o">==</span> <span class="n">other</span><span class="p">)</span>

</div>
<span class="nd">@deconstructible</span>
<div class="viewcode-block" id="URLValidator"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.URLValidator">[docs]</a><span class="k">class</span> <span class="nc">URLValidator</span><span class="p">(</span><span class="n">RegexValidator</span><span class="p">):</span>
    <span class="n">regex</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span>
        <span class="s">r&#39;^(?:[a-z0-9\.\-]*)://&#39;</span>  <span class="c"># scheme is validated separately</span>
        <span class="s">r&#39;(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}(?&lt;!-)\.?)|&#39;</span>  <span class="c"># domain...</span>
        <span class="s">r&#39;localhost|&#39;</span>  <span class="c"># localhost...</span>
        <span class="s">r&#39;\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|&#39;</span>  <span class="c"># ...or ipv4</span>
        <span class="s">r&#39;\[?[A-F0-9]*:[A-F0-9:]+\]?)&#39;</span>  <span class="c"># ...or ipv6</span>
        <span class="s">r&#39;(?::\d+)?&#39;</span>  <span class="c"># optional port</span>
        <span class="s">r&#39;(?:/?|[/?]\S+)$&#39;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Enter a valid URL.&#39;</span><span class="p">)</span>
    <span class="n">schemes</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;http&#39;</span><span class="p">,</span> <span class="s">&#39;https&#39;</span><span class="p">,</span> <span class="s">&#39;ftp&#39;</span><span class="p">,</span> <span class="s">&#39;ftps&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">schemes</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">URLValidator</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">schemes</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">schemes</span> <span class="o">=</span> <span class="n">schemes</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">force_text</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="c"># Check first if the scheme is valid</span>
        <span class="n">scheme</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;://&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">scheme</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">schemes</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>

        <span class="c"># Then check full URL</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="nb">super</span><span class="p">(</span><span class="n">URLValidator</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__call__</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">ValidationError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="c"># Trivial case failed. Try for possible IDN domain</span>
            <span class="k">if</span> <span class="n">value</span><span class="p">:</span>
                <span class="n">scheme</span><span class="p">,</span> <span class="n">netloc</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">query</span><span class="p">,</span> <span class="n">fragment</span> <span class="o">=</span> <span class="n">urlsplit</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">netloc</span> <span class="o">=</span> <span class="n">netloc</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;idna&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">&#39;ascii&#39;</span><span class="p">)</span>  <span class="c"># IDN -&gt; ACE</span>
                <span class="k">except</span> <span class="ne">UnicodeError</span><span class="p">:</span>  <span class="c"># invalid domain part</span>
                    <span class="k">raise</span> <span class="n">e</span>
                <span class="n">url</span> <span class="o">=</span> <span class="n">urlunsplit</span><span class="p">((</span><span class="n">scheme</span><span class="p">,</span> <span class="n">netloc</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">query</span><span class="p">,</span> <span class="n">fragment</span><span class="p">))</span>
                <span class="nb">super</span><span class="p">(</span><span class="n">URLValidator</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__call__</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">url</span> <span class="o">=</span> <span class="n">value</span>

</div>
<span class="k">def</span> <span class="nf">validate_integer</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="nb">int</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="k">except</span> <span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">TypeError</span><span class="p">):</span>
        <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Enter a valid integer.&#39;</span><span class="p">),</span> <span class="n">code</span><span class="o">=</span><span class="s">&#39;invalid&#39;</span><span class="p">)</span>


<span class="nd">@deconstructible</span>
<span class="k">class</span> <span class="nc">EmailValidator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Enter a valid email address.&#39;</span><span class="p">)</span>
    <span class="n">code</span> <span class="o">=</span> <span class="s">&#39;invalid&#39;</span>
    <span class="n">user_regex</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span>
        <span class="s">r&quot;(^[-!#$%&amp;&#39;*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&amp;&#39;*+/=?^_`{}|~0-9A-Z]+)*$&quot;</span>  <span class="c"># dot-atom</span>
        <span class="s">r&#39;|^&quot;([\001-\010\013\014\016-\037!#-\[\]-\177]|</span><span class="se">\\</span><span class="s">[\001-\011\013\014\016-\177])*&quot;$)&#39;</span><span class="p">,</span>  <span class="c"># quoted-string</span>
        <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span>
    <span class="n">domain_regex</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span>
        <span class="s">r&#39;(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}|[A-Z0-9-]{2,}(?&lt;!-))$&#39;</span><span class="p">,</span>
        <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span>
    <span class="n">literal_regex</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span>
        <span class="c"># literal form, ipv4 or ipv6 address (SMTP 4.1.3)</span>
        <span class="s">r&#39;\[([A-f0-9:\.]+)\]$&#39;</span><span class="p">,</span>
        <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span>
    <span class="n">domain_whitelist</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;localhost&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">whitelist</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">message</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">message</span>
        <span class="k">if</span> <span class="n">code</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">code</span> <span class="o">=</span> <span class="n">code</span>
        <span class="k">if</span> <span class="n">whitelist</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">domain_whitelist</span> <span class="o">=</span> <span class="n">whitelist</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">force_text</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">value</span> <span class="ow">or</span> <span class="s">&#39;@&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>

        <span class="n">user_part</span><span class="p">,</span> <span class="n">domain_part</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">rsplit</span><span class="p">(</span><span class="s">&#39;@&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">user_regex</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">user_part</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">domain_part</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">domain_whitelist</span> <span class="ow">and</span>
                <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">validate_domain_part</span><span class="p">(</span><span class="n">domain_part</span><span class="p">)):</span>
            <span class="c"># Try for possible IDN domain-part</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">domain_part</span> <span class="o">=</span> <span class="n">domain_part</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;idna&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">&#39;ascii&#39;</span><span class="p">)</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">validate_domain_part</span><span class="p">(</span><span class="n">domain_part</span><span class="p">):</span>
                    <span class="k">return</span>
            <span class="k">except</span> <span class="ne">UnicodeError</span><span class="p">:</span>
                <span class="k">pass</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">validate_domain_part</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">domain_part</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">domain_regex</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">domain_part</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">True</span>

        <span class="n">literal_match</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">literal_regex</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">domain_part</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">literal_match</span><span class="p">:</span>
            <span class="n">ip_address</span> <span class="o">=</span> <span class="n">literal_match</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">validate_ipv46_address</span><span class="p">(</span><span class="n">ip_address</span><span class="p">)</span>
                <span class="k">return</span> <span class="bp">True</span>
            <span class="k">except</span> <span class="n">ValidationError</span><span class="p">:</span>
                <span class="k">pass</span>
        <span class="k">return</span> <span class="bp">False</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">EmailValidator</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">domain_whitelist</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">domain_whitelist</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">message</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>

<span class="n">validate_email</span> <span class="o">=</span> <span class="n">EmailValidator</span><span class="p">()</span>

<span class="n">slug_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;^[-a-zA-Z0-9_]+$&#39;</span><span class="p">)</span>
<span class="n">validate_slug</span> <span class="o">=</span> <span class="n">RegexValidator</span><span class="p">(</span><span class="n">slug_re</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s">&quot;Enter a valid &#39;slug&#39; consisting of letters, numbers, underscores or hyphens.&quot;</span><span class="p">),</span> <span class="s">&#39;invalid&#39;</span><span class="p">)</span>

<span class="n">ipv4_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$&#39;</span><span class="p">)</span>
<span class="n">validate_ipv4_address</span> <span class="o">=</span> <span class="n">RegexValidator</span><span class="p">(</span><span class="n">ipv4_re</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Enter a valid IPv4 address.&#39;</span><span class="p">),</span> <span class="s">&#39;invalid&#39;</span><span class="p">)</span>


<div class="viewcode-block" id="validate_ipv6_address"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.validate_ipv6_address">[docs]</a><span class="k">def</span> <span class="nf">validate_ipv6_address</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">is_valid_ipv6_address</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
        <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Enter a valid IPv6 address.&#39;</span><span class="p">),</span> <span class="n">code</span><span class="o">=</span><span class="s">&#39;invalid&#39;</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="validate_ipv46_address"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.validate_ipv46_address">[docs]</a><span class="k">def</span> <span class="nf">validate_ipv46_address</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">validate_ipv4_address</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">ValidationError</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">validate_ipv6_address</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">ValidationError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Enter a valid IPv4 or IPv6 address.&#39;</span><span class="p">),</span> <span class="n">code</span><span class="o">=</span><span class="s">&#39;invalid&#39;</span><span class="p">)</span>
</div>
<span class="n">ip_address_validator_map</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s">&#39;both&#39;</span><span class="p">:</span> <span class="p">([</span><span class="n">validate_ipv46_address</span><span class="p">],</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Enter a valid IPv4 or IPv6 address.&#39;</span><span class="p">)),</span>
    <span class="s">&#39;ipv4&#39;</span><span class="p">:</span> <span class="p">([</span><span class="n">validate_ipv4_address</span><span class="p">],</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Enter a valid IPv4 address.&#39;</span><span class="p">)),</span>
    <span class="s">&#39;ipv6&#39;</span><span class="p">:</span> <span class="p">([</span><span class="n">validate_ipv6_address</span><span class="p">],</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Enter a valid IPv6 address.&#39;</span><span class="p">)),</span>
<span class="p">}</span>


<span class="k">def</span> <span class="nf">ip_address_validators</span><span class="p">(</span><span class="n">protocol</span><span class="p">,</span> <span class="n">unpack_ipv4</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Depending on the given parameters returns the appropriate validators for</span>
<span class="sd">    the GenericIPAddressField.</span>

<span class="sd">    This code is here, because it is exactly the same for the model and the form field.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">protocol</span> <span class="o">!=</span> <span class="s">&#39;both&#39;</span> <span class="ow">and</span> <span class="n">unpack_ipv4</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s">&quot;You can only use `unpack_ipv4` if `protocol` is set to &#39;both&#39;&quot;</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">ip_address_validator_map</span><span class="p">[</span><span class="n">protocol</span><span class="o">.</span><span class="n">lower</span><span class="p">()]</span>
    <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;The protocol &#39;</span><span class="si">%s</span><span class="s">&#39; is unknown. Supported: </span><span class="si">%s</span><span class="s">&quot;</span>
                         <span class="o">%</span> <span class="p">(</span><span class="n">protocol</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">ip_address_validator_map</span><span class="p">)))</span>

<span class="n">comma_separated_int_list_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">&#39;^[\d,]+$&#39;</span><span class="p">)</span>
<span class="n">validate_comma_separated_integer_list</span> <span class="o">=</span> <span class="n">RegexValidator</span><span class="p">(</span><span class="n">comma_separated_int_list_re</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Enter only digits separated by commas.&#39;</span><span class="p">),</span> <span class="s">&#39;invalid&#39;</span><span class="p">)</span>


<span class="nd">@deconstructible</span>
<span class="k">class</span> <span class="nc">BaseValidator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="n">compare</span> <span class="o">=</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">a</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">b</span>
    <span class="n">clean</span> <span class="o">=</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Ensure this value is </span><span class="si">%(limit_value)s</span><span class="s"> (it is </span><span class="si">%(show_value)s</span><span class="s">).&#39;</span><span class="p">)</span>
    <span class="n">code</span> <span class="o">=</span> <span class="s">&#39;limit_value&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">limit_value</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">limit_value</span> <span class="o">=</span> <span class="n">limit_value</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">cleaned</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="n">params</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;limit_value&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">limit_value</span><span class="p">,</span> <span class="s">&#39;show_value&#39;</span><span class="p">:</span> <span class="n">cleaned</span><span class="p">}</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">compare</span><span class="p">(</span><span class="n">cleaned</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">limit_value</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">limit_value</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">limit_value</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">message</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>


<span class="nd">@deconstructible</span>
<div class="viewcode-block" id="MaxValueValidator"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.MaxValueValidator">[docs]</a><span class="k">class</span> <span class="nc">MaxValueValidator</span><span class="p">(</span><span class="n">BaseValidator</span><span class="p">):</span>
    <span class="n">compare</span> <span class="o">=</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">a</span> <span class="o">&gt;</span> <span class="n">b</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Ensure this value is less than or equal to </span><span class="si">%(limit_value)s</span><span class="s">.&#39;</span><span class="p">)</span>
    <span class="n">code</span> <span class="o">=</span> <span class="s">&#39;max_value&#39;</span>

</div>
<span class="nd">@deconstructible</span>
<div class="viewcode-block" id="MinValueValidator"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.MinValueValidator">[docs]</a><span class="k">class</span> <span class="nc">MinValueValidator</span><span class="p">(</span><span class="n">BaseValidator</span><span class="p">):</span>
    <span class="n">compare</span> <span class="o">=</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">a</span> <span class="o">&lt;</span> <span class="n">b</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Ensure this value is greater than or equal to </span><span class="si">%(limit_value)s</span><span class="s">.&#39;</span><span class="p">)</span>
    <span class="n">code</span> <span class="o">=</span> <span class="s">&#39;min_value&#39;</span>

</div>
<span class="nd">@deconstructible</span>
<div class="viewcode-block" id="MinLengthValidator"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.MinLengthValidator">[docs]</a><span class="k">class</span> <span class="nc">MinLengthValidator</span><span class="p">(</span><span class="n">BaseValidator</span><span class="p">):</span>
    <span class="n">compare</span> <span class="o">=</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">a</span> <span class="o">&lt;</span> <span class="n">b</span>
    <span class="n">clean</span> <span class="o">=</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">:</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">ungettext_lazy</span><span class="p">(</span>
        <span class="s">&#39;Ensure this value has at least </span><span class="si">%(limit_value)d</span><span class="s"> character (it has </span><span class="si">%(show_value)d</span><span class="s">).&#39;</span><span class="p">,</span>
        <span class="s">&#39;Ensure this value has at least </span><span class="si">%(limit_value)d</span><span class="s"> characters (it has </span><span class="si">%(show_value)d</span><span class="s">).&#39;</span><span class="p">,</span>
        <span class="s">&#39;limit_value&#39;</span><span class="p">)</span>
    <span class="n">code</span> <span class="o">=</span> <span class="s">&#39;min_length&#39;</span>

</div>
<span class="nd">@deconstructible</span>
<div class="viewcode-block" id="MaxLengthValidator"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.MaxLengthValidator">[docs]</a><span class="k">class</span> <span class="nc">MaxLengthValidator</span><span class="p">(</span><span class="n">BaseValidator</span><span class="p">):</span>
    <span class="n">compare</span> <span class="o">=</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">a</span> <span class="o">&gt;</span> <span class="n">b</span>
    <span class="n">clean</span> <span class="o">=</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">:</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">ungettext_lazy</span><span class="p">(</span>
        <span class="s">&#39;Ensure this value has at most </span><span class="si">%(limit_value)d</span><span class="s"> character (it has </span><span class="si">%(show_value)d</span><span class="s">).&#39;</span><span class="p">,</span>
        <span class="s">&#39;Ensure this value has at most </span><span class="si">%(limit_value)d</span><span class="s"> characters (it has </span><span class="si">%(show_value)d</span><span class="s">).&#39;</span><span class="p">,</span>
        <span class="s">&#39;limit_value&#39;</span><span class="p">)</span>
    <span class="n">code</span> <span class="o">=</span> <span class="s">&#39;max_length&#39;</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../index.html">Module code</a>
        
          <ul><li><a href="../../django.html">django</a>
        
        <ul><li>django.core.validators</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>